package stud.mpgame.policyserver;


import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import java.net.InetSocketAddress;

public class PolicyServer {

    public static final int POLICY_PORT = 843;

    public static void main(String[] args) throws Exception {
        bindPolicy();
    }

    private static void bindPolicy() throws Exception {
        NioSocketAcceptor acceptor = new NioSocketAcceptor();
        DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();

        chain.addLast("codec", new ProtocolCodecFilter(new PolicyProtocolCodecFactory()));

        addLogger(chain);

        acceptor.setHandler(new PolicyProtocolHandler());
        acceptor.bind(new InetSocketAddress(POLICY_PORT));

        System.out.println("Listening on port " + POLICY_PORT);
    }

    private static void addLogger(DefaultIoFilterChainBuilder chain) throws Exception {
        chain.addLast("logger", new LoggingFilter());
        System.out.println("Logging ON");
    }

}